home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / time.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  155 lines

  1. /***********************************************************************
  2.  * $Id: time.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *
  8.  * Simple (real) timing routine. The time itself has no meaning to
  9.  * the caller, but a diff between two calls does.
  10.  *
  11.  ***********************************************************************/
  12. #if !defined(lint) && defined(F_ID)
  13. char *id_tim = "$Id: time.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  14. #endif
  15.  
  16. #include "ulib.h"
  17. #include <sys/time.h>
  18.  
  19. static long lsec, lusec;
  20. static struct timeval now;
  21.  
  22. void
  23. reset_time(void)
  24. {
  25.     gettimeofday(&now, 0);
  26.     lsec = now.tv_sec;
  27.     lusec = now.tv_usec;
  28. }
  29.  
  30. /*************************************************************
  31.  * time passed in milli-second since last reset. Can't be longer
  32.  * than a few hundred hours due to overflows, but for all pratical
  33.  * purposes of measuring time difference, it is fine.
  34.  *************************************************************/
  35. long
  36. time_passed(void)
  37. {
  38.     gettimeofday(&now, 0);
  39.     return (now.tv_sec - lsec) * 1000 + (now.tv_usec - lusec) / 1000;
  40. }
  41.  
  42. double
  43. current_time(void)
  44. {
  45.     gettimeofday(&now, 0);
  46.     return ((double) now.tv_sec + (double) 0.000001 * now.tv_usec);
  47. }
  48.  
  49.  
  50. /************ Current date in ASCII *****************/
  51. #include <time.h>
  52. const char *
  53. current_ascii_date(void)
  54. {
  55.     time_t t = time(0);
  56.     return asctime(localtime(&t));
  57. }
  58.  
  59. /********************************************************************
  60.  * Given hour, min , day, month (0-11),
  61.  * and year return Unix time. The only thing we need is the start
  62.  * time of the month and year.
  63.  *******************************************************************/
  64. static time_t
  65. date2time(int mins, int hours, int days, int month, int year, int gmt)
  66. {
  67.     time_t min, hr, day;
  68.     long guess;
  69.     struct tm *t;
  70.     int delta;
  71.  
  72.     min = 60;
  73.     hr = 60 * min;
  74.     day = 24 * hr;
  75.  
  76.     if (year >= 1900)
  77.     year -= 1900;
  78.  
  79.     guess = time(0);        /* get current time */
  80.     t = (gmt ? gmtime : localtime) (&guess);
  81.     while ((delta = year - t->tm_year))
  82.       {
  83.       guess += delta * (364 * day);
  84.       t = (gmt ? gmtime : localtime) (&guess);
  85.       }
  86.  
  87.     while ((delta = month - t->tm_mon))
  88.       {
  89.       guess += delta * (28 * day);
  90.       t = (gmt ? gmtime : localtime) (&guess);
  91.       }
  92.  
  93.     /* this will be the time at the start of the month */
  94.     guess -= t->tm_sec + t->tm_min * min + t->tm_hour * hr +
  95.     (t->tm_mday - 1) * day;
  96.     guess += (days - 1) * day + mins * min + hours * hr;
  97.  
  98.     return guess;
  99. }
  100.  
  101. #if 0
  102. /****************************************************************
  103.  * Convert an ascii month to ordinal
  104.  ****************************************************************/
  105. static const char *months[] =
  106. {
  107.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  108.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  109. };
  110. static int
  111. month_to_num(const char *month)
  112. {
  113.     int i;
  114.  
  115.     for (i = 1; i <= 12 && strcasecmp(months[i], month); i++)
  116.     ;
  117.     return (i % 13);
  118. }
  119. #endif
  120. /*****************************************************************
  121.  * Given an output of date, convert to unix time.
  122.  * the date is generated by date '+%m %e %Y %H' for
  123.  * country-independence
  124.  *****************************************************************/
  125. time_t
  126. date_to_time(const char *date)
  127. {
  128.     static char mon[10] = "02", ahr[10] = "1";
  129.     int year = 1994, month = 2, day = 30, hr = 20;
  130.  
  131.     sscanf(date, "%s %d %d %s", mon, &day, &year, ahr);
  132.  
  133.     /* need to nuke the 0 in months and hr */
  134.     month = atol(mon + (mon[0] == 0));
  135.     hr = atol(ahr + (ahr[0] == 0));
  136.  
  137.     /* note month starts from 0! for date2time */
  138.     return date2time(0, hr, day, month - 1, year, 0);
  139. }
  140.  
  141. #ifdef TEST
  142. #include <stdio.h>
  143. main()
  144. {
  145.     char aa[1024];
  146.     fprintf(stderr, "current date: %s\n", current_ascii_date());
  147.     fprintf(stderr, "current time: %g\n", current_time());
  148.     fprintf(stderr, "current time: %ld\n", (long) time(0));
  149.     fprintf(stderr, "IN m e y t");
  150.     while (gets(aa))
  151.     fprintf(stderr, "%s=>%ld\n", aa, (long) date_to_time(aa, 1));
  152.  
  153. }
  154. #endif
  155.